import { _decorator, Component, input, instantiate, Node, Prefab, RigidBody2D, Vec2,Vec3,Input, Animation,AnimationClip, Label } from 'cc';
import { store } from './store';
import { Tween } from 'cc';
import { AudioManager } from './AudioManager';
import { UITransform } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('loading')
export class loading extends Component {
    // 管子的预制体
    @property(Prefab)
    public pipePrefab:Prefab = null
    // 管子生成的速度  单位毫秒
    @property(Number)
    public speed:number = 0
    // 游戏当前的状态 true:游戏正常进行 false: 游戏结束
    @property(Boolean)
    public static gameState:Boolean = true // 默认正常
    /* 小鸟 */
    @property(Node)
    public bird:Node = null
            // 刚体组件 
        public birdRgd:RigidBody2D = null
            // 动画组件
        public birdAni:Animation = null
        // 向上施加的力度大小
        @property(Number)
        public upSpeed:number = 0
        // 小鸟受到的重力规模
        @property(Number)
        public gravity:number = 0
    // 计分label
    @property(Node)
    public scoreLabel:Node = null
    protected onLoad(): void {
        // 触摸屏幕时，为小鸟施加一个y方向上的线速度
        input.on(Input.EventType.TOUCH_START,this.applyForceForBird,this)
    }

    start() {
        this.birdRgd = this.bird.getComponent(RigidBody2D) 
        this.birdAni = this.bird.getComponent(Animation)
        // 为小鸟添加重力
        this.birdRgd.gravityScale = this.gravity
        this.createPipe()
    }
    // 创建管子
    createPipe(){
        // 柱子生成：（x:772,y：自动生成[540-740]区间） 生成管子区间高度是高度的一半
        let contentSize = this.node.getComponent(UITransform).contentSize
        let width = contentSize.width * this.node.scaleX
        let height = contentSize.height * this.node.scaleY
        // 区间是-zone —— zone
        let zone = height * 0.25  * 0.5
        const randomHeight = (Math.random()*(zone + zone +1)) - zone
        // 随机生成540到940的随机数
        // const randomHeight = (Math.random()*(940 - 540 +1)) - 540
        if(this.pipePrefab){
            const newPipe = instantiate(this.pipePrefab)
            newPipe.setPosition(new Vec3(width/2 + 26, randomHeight,0))
            this.node.addChild(newPipe)
        }else{
            console.log('未获取到预制体')
        }
    }
    // 为小鸟施加一个向上的速度
    applyForceForBird(){
        this.birdRgd.linearVelocity = new Vec2(0,this.upSpeed)
        // 同时播放扑动翅膀的动画
        this.playBirdAnimationOnce()
        // 播放音频
        AudioManager.instance.playEffect('wing')
        // 设置角度
        this.bird.angle = 45
    }
    // 播放扑动翅膀动画
    playBirdAnimationOnce() {
        if (this.birdAni) {
            // 停止当前动画
            this.birdAni.stop();
            // 获取动画状态
            const state = this.birdAni.getState('birdFly');
            if (state) {
                // 设置动画只播放一次
                state.repeatCount = 1;
                // 播放动画
                this.birdAni.play('birdFly');
            } else {
                console.log('未找到动画状态 birdFly');
            }
        } else {
            console.log('未获取到 Animation 组件');
        }
    }
    count:number = 0
    update(deltaTime: number) {  
        // 判断游戏是否结束
        if(loading.gameState){
            if(store.score)
            // 更新分数
            this.scoreLabel.getComponent(Label).string = store.score.toString()
            this.bird.angle--
            // 提高游戏速度
            store.speed += 0.1
            this.count += deltaTime * store.speed
            if(this.count > 500){
                this.count = 0
                this.createPipe()
            }

        } else {
            // 更新最高得分
            store.bestScore = Math.max(store.bestScore,store.score)
            // 关闭触摸屏幕事件
            input.off(Input.EventType.TOUCH_START,this.applyForceForBird,this)
            // 启用节点
            this.node.parent.getChildByName('GameOver').active = true
            // 清空分数
            this.node.parent.getChildByName('ScoreNode').getComponent(Label).string = ''
        }
    }
    protected onDestroy(): void {
        // 重新设置当前的游戏状态
        loading.gameState = true
        store.score = 0
        // 解除事件监听
        input.off(Input.EventType.TOUCH_START,this.applyForceForBird,this)
        // 重置分数
        store.speed = 280
    }
}


